﻿var productsJsonData = null;

$(document).ready(function () {
    $("button, input:submit, .button").button();
    
    if ($('#price-calc-panel')) {
        initCalculator();
    }
});

function initCalculator() {
    $.getJSON("/" + culture + "/Product/PriceCalculatorJSON",
            function (data) {
                //addShowAllOption($('#product-types'));
                productsJsonData = new Object();
                $.each(data, function () {
                    var selectOptEl = $('<option/>').attr('value', this.Code);
                    selectOptEl.html(this.Name);
                    $('#product-types').append(selectOptEl);
                    $('#product-types-simple').append(selectOptEl.clone());
                    productsJsonData[this.Code] = this;
                });

                $('#product-types-simple').change(function () {
                    onProductSimpleTypeSelect();
                });
                $('#product-types-simple').change();

                $('#product-types').change(function () {
                    onProductTypeSelect();
                });
                $('#product-types').change();

            });
            $('#simple-switch').click(function () {
                $(this).addClass('switched');
                $('#pro-switch').removeClass('switched');
                $('#simple-mode').css('display', 'block');
                $('#pro-mode').css('display', 'none');
            });

            $('#pro-switch').click(function () {
                $(this).addClass('switched');
                $('#simple-switch').removeClass('switched');
                $('#pro-mode').css('display', 'block');
                $('#simple-mode').css('display', 'none');
            });
}

function onProductTypeSelect() {
    var prodName = $('select#product-types option:selected').val();
    $('#color-modes option').remove();
    $('#media-types option').remove();
    $('#form-factors option').remove();
    if (prodName == '') {
        return;
    }

    $.each(productsJsonData[prodName].Medias, function () {
        var selectOptEl = $('<option/>').attr('value', this.Code);
        selectOptEl.html(this.Name);
        $('#media-types').append(selectOptEl);
        if (this.Code == productsJsonData[prodName].FormFactors[0].DefaultPrintMedia) {
            selectOptEl.attr('selected', 'selected');
            selectOptEl.css('color', '#666');
        }
    });

    $.each(productsJsonData[prodName].Colors, function () {
        var selectOptEl = $('<option/>').attr('value', this.Code);
        selectOptEl.html(this.Name);
        $('#color-modes').append(selectOptEl);
    });

    $.each(productsJsonData[prodName].FormFactors, function () {
        var selectOptEl = $('<option/>').attr('value', this.Id);
        selectOptEl.html(this.Name + ' (' + this.SizeInfo +')');
        $('#form-factors').append(selectOptEl);
    });

    //form - factors
}

function onProductSimpleTypeSelect() {
    
    var prodName = $('select#product-types-simple option:selected').val();
    
    if (prodName == '') {
        return;
    }

    
    $('#form-factors-cont').empty();
    $.each(productsJsonData[prodName].FormFactors, function (idx, data) {
        var labelEl = $('<label/>').attr('for', 'FFactor' + data.Id)
                                   .attr('title', data.SizeInfo);
        var radioEl = $('<input/>').attr('type', 'radio')
                                   .attr('name', 'FormFactorId')
                                   .attr('value', data.Id)
                                   .attr('id', 'FFactor' + data.Id);
        labelEl.html(data.Name);

        $('#form-factors-cont').append(radioEl);
        radioEl.click(function () {
            $('#default-print-media').val(data.DefaultPrintMedia);
        });
        $('#form-factors-cont').append(labelEl);
    });
    $('input[type="radio"][name="FormFactorId"]:first').click();

    $('#ColorModeMode4x0').attr('disabled','disabled');
    $('#ColorModeMode4x4').attr('disabled', 'disabled');

    var isChecked = false;
    $.each(productsJsonData[prodName].Colors, function () {
        if (this.Code == 2) {
            $('#ColorModeMode4x4').removeAttr('disabled');
            if (!isChecked) {
                $('#ColorModeMode4x4').click();
            }
        } else if (this.Code == 1) {
            $('#ColorModeMode4x0').removeAttr('disabled');
            $('#ColorModeMode4x0').click();
            isChecked = true;
        }
    });
}


function addShowAllOption(selectEl) {
    var showAllEl = $('<option/>').attr('value', "").attr('selected', 'selected');
    showAllEl.html("all");
    selectEl.append(showAllEl);
}